package Demo1;

class Money implements Cloneable{
    public double money = 12.25;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}

class Student implements Cloneable{//这是一个空接口（”标记接口“）。作用为”表示当前对象是可以被克隆的“
    public String name;
    public Money m = new Money();

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                '}';
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        Student student = (Student) super.clone();
        student.m = (Money) this.m.clone();
        return student;
    }

}

public class Test {
    public static void main(String[] args) throws CloneNotSupportedException {//这是一个编译时就发生的异常，在此处宣称
        Student student1 = new Student();
        Student student2 = (Student)student1.clone();
        System.out.println(student1.m.money);
        System.out.println(student2.m.money);
        System.out.println("=========================");
        student2.m.money = 99;//克隆了一份student1得到student2，改变student2的值
        System.out.println(student1.m.money);
        System.out.println(student2.m.money);
    }
}

